package com.shuoyue.ycgk.ui.course;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.CourseInfo;
import com.shuoyue.ycgk.entity.TimerTable;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.course.contract.CourseContract;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;

/**
 * 首页课程数据片段
 */
public class FragmengCourseItem extends BaseMvpFragment<CourseContract.Presenter> implements CourseContract.CourseListView {
    Type baseType;
    Type topType;
    String title;
    @BindView(R.id.newsRecycleView)
    RecyclerView newsRecycleView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    CourseAdapter courseAdapter;

    ListWithPage listWithPage;

    @Override
    public void initParams(Bundle bundle) {
        baseType = (Type) bundle.getSerializable("baseType");
        topType = (Type) bundle.getSerializable("topType");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_course_item;
    }

    @Override
    protected void initView() {
        newsRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        newsRecycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        courseAdapter = new CourseAdapter(null);
        newsRecycleView.setAdapter(courseAdapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            listWithPage = null;
            if (topType != null)
                mPresenter.getCourseList(topType.getId(), topType.getSubjectId(), 1);
            refreshLayout.finishRefresh(2000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            if (topType != null)
                mPresenter.getCourseList(topType.getId(), topType.getSubjectId(), listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore(2000);
        });
        courseAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Course course = (Course) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_all:
                    if (course.getStatus() == 1) {
                        startActivity(new Intent(mContext, CourseInfoActivity.class)
                                .putExtra("id", course.getId()));
                    } else {
                        toast("课程正在上架，请关注上架后购买");
                    }

                    break;
            }
        });
        registEmptyView(courseAdapter);

    }

    @Override
    protected void initData() {
        mPresenter = new CourseContract.Presenter();
        mPresenter.attachView(this);
        if (topType != null) {
            mPresenter.getCourseList(topType.getId(), topType.getSubjectId(), 1);
        }
    }

    public static FragmengCourseItem getInstance(Type baseType,
                                                 Type topType) {
        FragmengCourseItem fragment = new FragmengCourseItem();
        fragment.title = topType.getName();
        Bundle bundle = new Bundle();
        bundle.putSerializable("baseType", baseType);
        bundle.putSerializable("topType", topType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setCourseList(ListWithPage<Course> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            courseAdapter.resetData(list.getRecords());
        } else {
            courseAdapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages()&& list.getPages() > 0);

        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setCourseInfo(CourseInfo info) {

    }

    @Override
    public void setVideoInfo(TimerTable timerTable, TimerTable adapterData) {

    }

    @Override
    public void notBuyNotice() {

    }
}
